﻿using CGT.GOP.App_Start;
using CGT.GOP.Common;
using CGT.Infrastructure.Dto;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Repository;
using CGT.Infrastructure.Service;
using Dapper.Extensions;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CGT.GOP.Controllers
{
    public class LogController : BaseController
    {
        GovLogLoginService llService = new GovLogLoginService();
        GovLogOperateService loService = new GovLogOperateService();
        GovLogSafeService lsService = new GovLogSafeService();
        private int LoginMenuCode = 10201;
        private int OperateMenuCode = 10203;
        private int SafeMenuCode = 10202;

        #region 登录日志
        [Authentication(MenuCode = 10201)]
        public ActionResult LoginList()
        {
            loService.Info(CurrentUser.UserId, "查看登录日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), LoginMenuCode);
            lsService.Info(CurrentUser.UserId, "查看登录日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), LoginMenuCode);

            return View();
        }

        /// <summary>
        /// 日志详情
        /// </summary>
        /// <param name="logId"></param>
        /// <returns></returns>
        public ActionResult LogLoginInfo(long logId)
        {
            ScLogLogin scLogin = new ScLogLogin();
            scLogin.PageIndex = 1;
            scLogin.PageSize = 1;
            scLogin.LogId = logId;
            Page<GovLogLoginDto> page = llService.GetList(scLogin);
            return View(page.rows[0]);
        }

        /// <summary>
        /// 统计报表
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginChart()
        {
            loService.Info(CurrentUser.UserId, "查看登录日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), LoginMenuCode);
            lsService.Info(CurrentUser.UserId, "查看登录日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), LoginMenuCode);
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetPageList()
        {
            ScLogLogin scLogin = new ScLogLogin();
            scLogin.PageIndex = GetPageIndex();
            scLogin.PageSize = GetPageSize();
            scLogin.IncidentType = EConvert.ToInt(GetParam("selIncidentType"),0);
            scLogin.StartDate = GetParam("txtStartDate");
            scLogin.EndDate = GetParam("txtEndDate");
            Page<GovLogLoginDto> page = llService.GetList(scLogin);
            return ReturnResult(new ResultPageData<List<GovLogLoginDto>>
            {
                data = page.rows,
                total = page.total
            });
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult ImportDataToExcel()
        {
            ResultData<string> rt = new ResultData<string>();
            ScLogLogin scLogin = new ScLogLogin();
            scLogin.PageIndex = 1;
            scLogin.PageSize = int.MaxValue;
            scLogin.IncidentType = EConvert.ToInt(GetParam("selIncidentType"), 0);
            scLogin.StartDate = GetParam("txtStartDate");
            scLogin.EndDate = GetParam("txtEndDate");
            llService.ImportDataToExcel(scLogin);
            return ReturnResult(rt);
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetLoginStatisicData()
        {
            ResultData<List<LoginStatisicDto>> rt = new ResultData<List<LoginStatisicDto>>();
            ScLogLogin scLogin = new ScLogLogin();
            scLogin.IncidentType = EConvert.ToInt(GetParam("selIncidentType"), 0);
            scLogin.StartDate = GetParam("txtStartDate");
            scLogin.EndDate = GetParam("txtEndDate");
            rt.data = llService.SearchStatisticalInformation(scLogin);
            return ReturnResult(rt);
        }

        #endregion

        #region 操作日志
        [Authentication(MenuCode = 10203)]
        public ActionResult OperateList()
        {
            loService.Info(CurrentUser.UserId, "查看操作日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), OperateMenuCode);
            lsService.Info(CurrentUser.UserId, "查看操作日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), OperateMenuCode);

            return View();
        }

        /// <summary>
        /// 日志详情
        /// </summary>
        /// <param name="logId"></param>
        /// <returns></returns>
        public ActionResult LogOperateInfo(long logId)
        {
            ScLogOperate scOperate = new ScLogOperate();
            scOperate.PageIndex = 1;
            scOperate.PageSize = 1;
            scOperate.LogId = logId;
            Page<GovLogOperateDto> page = loService.GetList(scOperate);
            return View(page.rows[0]);
        }

        /// <summary>
        /// 统计报表
        /// </summary>
        /// <returns></returns>
        public ActionResult OperateChart()
        {
            loService.Info(CurrentUser.UserId, "查看操作日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), OperateMenuCode);
            lsService.Info(CurrentUser.UserId, "查看操作日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), OperateMenuCode);

            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetLogOperatePageList()
        {
            ScLogOperate scOperate = new ScLogOperate();
            scOperate.PageIndex = GetPageIndex();
            scOperate.PageSize = GetPageSize();
            scOperate.MenuId = EConvert.ToInt(GetParam("selMenuId"), 0);
            scOperate.MenuSubId = EConvert.ToInt(GetParam("selMenuSubId"), 0);
            scOperate.StartDate = GetParam("txtStartDate");
            scOperate.EndDate = GetParam("txtEndDate");
            Page<GovLogOperateDto> page = loService.GetList(scOperate);
            return ReturnResult(new ResultPageData<List<GovLogOperateDto>>
            {
                data = page.rows,
                total = page.total
            });
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult ImportLogOperateDataToExcel()
        {
            ResultData<string> rt = new ResultData<string>();
            try
            {
                ScLogOperate scOperate = new ScLogOperate();
                scOperate.MenuId = EConvert.ToInt(GetParam("selMenuId"), 0);
                scOperate.MenuSubId = EConvert.ToInt(GetParam("selMenuSubId"), 0);
                scOperate.StartDate = GetParam("txtStartDate");
                scOperate.EndDate = GetParam("txtEndDate");
                scOperate.PageIndex = 1;
                scOperate.PageSize = int.MaxValue;
                loService.ImportDataToExcel(scOperate);
            }
            catch (Exception ex)
            {
                rt.status = -1;
                rt.message = ex.Message;
            }
            return ReturnResult(rt);
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetOperateStatisicData()
        {
            ResultData<List<LogOperateStatisticDto>> rt = new ResultData<List<LogOperateStatisticDto>>();
            ScLogOperate scOperate = new ScLogOperate();
            scOperate.StartDate = GetParam("txtStartDate");
            scOperate.EndDate = GetParam("txtEndDate");
            rt.data = loService.SearchStatisticalInformation(scOperate);
            return ReturnResult(rt);
        }
        #endregion

        #region 安全日志
        [Authentication(MenuCode = 10202)]
        public ActionResult SafeList()
        {
            loService.Info(CurrentUser.UserId, "查看安全日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), SafeMenuCode);
            lsService.Info(CurrentUser.UserId, "查看安全日志", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), SafeMenuCode);

            return View();
        }

        /// <summary>
        /// 日志详情
        /// </summary>
        /// <param name="logId"></param>
        /// <returns></returns>
        public ActionResult LogSafeInfo(long logId)
        {
            ScLogSafe scSafe = new ScLogSafe();
            scSafe.PageIndex = 1;
            scSafe.PageSize = 1;
            scSafe.LogId = logId;
            scSafe.IsConventional = -1;
            scSafe.IsCoreFunction = -1;
            Page<GovLogSafeDto> page = lsService.GetList(scSafe);
            return View(page.rows[0]);
        }

        /// <summary>
        /// 统计报表
        /// </summary>
        /// <returns></returns>
        public ActionResult SafeChart()
        {
            loService.Info(CurrentUser.UserId, "查看安全日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), SafeMenuCode);
            lsService.Info(CurrentUser.UserId, "查看安全日志分析图", 4, "", string.Format("查询条件:开始时间-{0},结束时间-{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), SafeMenuCode);

            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetLogSafePageList()
        {
            ScLogSafe scSafe = new ScLogSafe();
            scSafe.PageIndex = GetPageIndex();
            scSafe.PageSize = GetPageSize();
            scSafe.MenuId = EConvert.ToInt(GetParam("selMenuId"), 0);
            scSafe.MenuSubId = EConvert.ToInt(GetParam("selMenuSubId"), 0);
            scSafe.IsCoreFunction = EConvert.ToInt(GetParam("selCoreFunction"), -1);
            scSafe.IsConventional = EConvert.ToInt(GetParam("selConventional"), -1);
            scSafe.StartDate = GetParam("txtStartDate");
            scSafe.EndDate = GetParam("txtEndDate");
            Page<GovLogSafeDto> page = lsService.GetList(scSafe);
            return ReturnResult(new ResultPageData<List<GovLogSafeDto>>
            {
                data = page.rows,
                total = page.total
            });
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult ImportLogSafeDataToExcel()
        {
            ResultData<string> rt = new ResultData<string>();
            ScLogSafe scSafe = new ScLogSafe();
            scSafe.MenuId = EConvert.ToInt(GetParam("selMenuId"), 0);
            scSafe.MenuSubId = EConvert.ToInt(GetParam("selMenuSubId"), 0);
            scSafe.IsCoreFunction = EConvert.ToInt(GetParam("selCoreFunction"), -1);
            scSafe.IsConventional = EConvert.ToInt(GetParam("selUnconventional"), -1);
            scSafe.StartDate = GetParam("txtStartDate");
            scSafe.EndDate = GetParam("txtEndDate");
            scSafe.PageIndex = 1;
            scSafe.PageSize = int.MaxValue;
            lsService.ImportDataToExcel(scSafe);
            return ReturnResult(rt);
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetSafeStatisicData()
        {
            ResultData<List<LogSafeStatisticDto>> rt = new ResultData<List<LogSafeStatisticDto>>();
            ScLogSafe scSafe = new ScLogSafe();
            scSafe.StartDate = GetParam("txtStartDate");
            scSafe.EndDate = GetParam("txtEndDate");
            scSafe.IsCoreFunction = EConvert.ToInt(GetParam("selCoreFunction"), -1);
            scSafe.IsConventional = EConvert.ToInt(GetParam("selConventional"), -1);
            rt.data = lsService.SearchStatisticalInformation(scSafe);
            return ReturnResult(rt);
        }
        #endregion

    }
}